package com.example.lj.uav.utils;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Pair;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lj.uav.MyApplication;

public class OnUIUtils {

    private static final int MESSAGE_TOAST = 1;
    private static final int MESSAGE_TEXTVIEW_TEXT = 2;
    private static final int MESSAGE_VIEW_REC = 3;
    private static final int MESSAGE_VIEW_VISIBLE = 4;
    private static final int MESSAGE_COMPOUNDBUTTON_STATE = 5;
    private static final int MESSAGE_EDITTEXT_TEXT = 6;
    private static final int MESSAGE_SPINNER_SELECTION = 7;
    private static final int MESSAGE_SEEKBAR_PROGRESS = 8;
    private static final int MESSAGE_PROGRESSBAR_PROGRESS = 9;
    private static final int MESSAGE_BACKGROUND = 10;
    private static final int MESSAGE_TEXTVIEW_COLOR = 11;

    private static Handler mUIHandler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message msg) {
            switch ((msg.what)) {
                case MESSAGE_TOAST:
                    Toast.makeText(MyApplication.getInstance(), (String) msg.obj, Toast.LENGTH_SHORT).show();
                    break;
                case MESSAGE_TEXTVIEW_TEXT:
                    Pair<TextView, String> p2 = (Pair<TextView, String>) msg.obj;
                    if (p2 != null) {
                        if (p2.first != null) {
                            p2.first.setText(p2.second);
                        }
                    }
                    break;
                case MESSAGE_VIEW_REC:
                    Pair<View, Integer> p3 = (Pair<View, Integer>) msg.obj;
                    if (p3 != null) {
                        if (p3.first != null) {
                            p3.first.setBackgroundResource(p3.second);
                        }
                    }
                    break;
                case MESSAGE_VIEW_VISIBLE:
                    Pair<View, Integer> p4 = (Pair<View, Integer>) msg.obj;
                    if (p4 != null) {
                        if (p4.first != null) {
                            p4.first.setVisibility(p4.second);
                        }
                    }
                    break;
                case MESSAGE_COMPOUNDBUTTON_STATE:
                    Pair<CompoundButton, Boolean> p5 = (Pair<CompoundButton, Boolean>) msg.obj;
                    if (p5 != null) {
                        if (p5.first != null) {
                            p5.first.setChecked(p5.second);
                        }
                    }
                    break;
                case MESSAGE_EDITTEXT_TEXT:
                    Pair<EditText, CharSequence> p6 = (Pair<EditText, CharSequence>) msg.obj;
                    if (p6 != null) {
                        if (p6.first != null) {
                            p6.first.setText(p6.second);
                        }
                    }
                    break;
                case MESSAGE_SPINNER_SELECTION:
                    Pair<Spinner, Integer> p7 = (Pair<Spinner, Integer>) msg.obj;
                    if (p7 != null) {
                        if (p7.first != null) {
                            p7.first.setSelection(p7.second);
                        }
                    }
                    break;
                case MESSAGE_SEEKBAR_PROGRESS:
                    Pair<SeekBar, Integer> p8 = (Pair<SeekBar, Integer>) msg.obj;
                    if (p8 != null) {
                        if (p8.first != null) {
                            p8.first.setProgress(p8.second);
                        }
                    }
                    break;
                case MESSAGE_PROGRESSBAR_PROGRESS:
                    Pair<ProgressBar, Integer> p9 = (Pair<ProgressBar, Integer>) msg.obj;
                    if (p9 != null) {
                        if (p9.first != null) {
                            p9.first.setProgress(p9.second);
                        }
                    }
                    break;
                case MESSAGE_BACKGROUND:
                    Pair<View, Integer> p10 = (Pair<View, Integer>) msg.obj;
                    if (p10 != null) {
                        if (p10.first != null) {
                            p10.first.setBackgroundResource(p10.second);
                        }
                    }
                    break;
                case MESSAGE_TEXTVIEW_COLOR:
                    Pair<TextView, Integer> p11 = (Pair<TextView, Integer>) msg.obj;
                    if (p11 != null) {
                        if (p11.first != null) {
                            p11.first.setTextColor(p11.second);
                        }
                    }
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    };

    /**
     * Toast
     *
     * @param string 内容
     */
    public static void setToast(final String string) {
        Message msg = new Message();
        msg.what = MESSAGE_TOAST;
        msg.obj = string;
        mUIHandler.sendMessage(msg);
    }

    /**
     * 修改TextView内容
     *
     * @param tv TextView
     * @param s  内容
     */
    public static void setTextView(final TextView tv, final String s) {
        Message msg = new Message();
        msg.what = MESSAGE_TEXTVIEW_TEXT;
        msg.obj = new Pair<>(tv, s);
        mUIHandler.sendMessage(msg);
    }

    /**
     * 修改TextView颜色
     *
     * @param tv    TextView
     * @param color 颜色
     */
    public static void setTextViewColor(final TextView tv, final int color) {
        Message msg = new Message();
        msg.what = MESSAGE_TEXTVIEW_COLOR;
        msg.obj = new Pair<>(tv, color);
        mUIHandler.sendMessage(msg);
    }

    /**
     * 修改背景
     *
     * @param v   View
     * @param rec 资源
     */
    public static void setViewRec(final View v, final int rec) {
        Message msg = new Message();
        msg.what = MESSAGE_VIEW_REC;
        msg.obj = new Pair<>(v, rec);
        mUIHandler.sendMessage(msg);
    }

    /**
     * 修改可见性
     *
     * @param v   View
     * @param vis 可见性
     */
    public static void setViewVisibility(final View v, final int vis) {
        Message msg = new Message();
        msg.what = MESSAGE_VIEW_VISIBLE;
        msg.obj = new Pair<>(v, vis);
        mUIHandler.sendMessage(msg);
    }

    /**
     * 修改CompoundButton状态
     *
     * @param v CompoundButton
     * @param b 状态
     */
    public static void setCompoundButtonState(final CompoundButton v, final boolean b) {
        Message msg = new Message();
        msg.what = MESSAGE_COMPOUNDBUTTON_STATE;
        msg.obj = new Pair<>(v, b);
        mUIHandler.sendMessage(msg);
    }

    /**
     * EditText
     *
     * @param v EditText
     * @param i CharSequence
     */
    public static void setEditViewText(final EditText v, final CharSequence i) {
        Message msg = new Message();
        msg.what = MESSAGE_EDITTEXT_TEXT;
        msg.obj = new Pair<>(v, i);
        mUIHandler.sendMessage(msg);
    }

    public static void setSpinnerSelection(final Spinner sp, final int position) {
        Message message = mUIHandler.obtainMessage();
        message.what = MESSAGE_SPINNER_SELECTION;
        message.obj = new Pair<>(sp, position);
        mUIHandler.sendMessage(message);
    }

    /**
     * Seekbar
     *
     * @param v
     * @param progress
     */
    public static void setSeekbarProgress(final SeekBar v, final int progress) {
        Message message = mUIHandler.obtainMessage();
        message.what = MESSAGE_SEEKBAR_PROGRESS;
        message.obj = new Pair<>(v, progress);
        mUIHandler.sendMessage(message);
    }

    /**
     * ProgressBar
     *
     * @param pb
     * @param p
     */
    public static void setProgressBarProgress(final ProgressBar pb, final int p) {
        Message message = mUIHandler.obtainMessage();
        message.what = MESSAGE_PROGRESSBAR_PROGRESS;
        message.obj = new Pair<>(pb, p);
        mUIHandler.sendMessage(message);
    }

    /**
     * View
     *
     * @param v
     * @param r
     */
    public static void setBackground(final View v, final int r) {
        Message message = mUIHandler.obtainMessage();
        message.what = MESSAGE_BACKGROUND;
        message.obj = new Pair<>(v, r);
        mUIHandler.sendMessage(message);
    }
}
